<template>
  <Form ref="form" :model="form" :rules="rule" :label-width="100">
    <FormItem label="药店名称" prop="name">
      <Input v-model="form.name" placeholder=""></Input>
    </FormItem>
    <FormItem label="省份" prop="province">
      <Select v-model="form.province">
        <Option value="广西">广西</Option>
      </Select>
    </FormItem>
    <FormItem label="市" prop="city">
      <Select v-model="form.city">
        <Option value="南宁">南宁</Option>
      </Select>
    </FormItem>
    <FormItem label="区域" prop="district">
      <Select v-model="form.district">
          <Option value="青秀区">青秀区</Option>
          <Option value="江南区">江南区</Option>
          <Option value="西乡塘区">西乡塘区</Option>
          <Option value="兴宁区">兴宁区</Option>
          <Option value="良庆区">良庆区</Option>
          <Option value="经开区">经开区</Option>
      </Select>
    </FormItem>
    <FormItem label="负责人" prop="leader">
      <Input v-model="form.leader" placeholder=""></Input>
    </FormItem>
    <FormItem label="负责人电话" prop="leaderMobilePhone">
      <Input v-model="form.leaderMobilePhone" placeholder=""></Input>
    </FormItem>
    <FormItem label="联系方式" prop="telephone">
      <Input v-model="form.telephone" placeholder=""></Input>
    </FormItem>
    <FormItem label="地址" prop="address">
      <Input v-model="form.address" placeholder=""></Input>
    </FormItem>
    <FormItem label="营业时间" prop="businessHours">
      <Input v-model="form.businessHours" placeholder=""></Input>
    </FormItem>
    <FormItem label="经纬度" prop="lngLat">
      <Input v-model="form.lngLat" placeholder=""></Input>
    </FormItem>
    <FormItem label="备注" prop="remark">
      <Input v-model="form.remark" placeholder=""></Input>
    </FormItem>
  </Form>
  </template>
<script>
import { save, update, get } from '@/api/wzyy/wzyy.shop.service.js'

export default {
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    params: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      form: {
        id: 0,
        name: '',
        province: '',
        city: '',
        district: '青秀区',
        telephone: '',
        address: '',
        businessHours: '',
        lngLat: '',
        remark: '',
        leaderMobilePhone: '',
        leader: ''
      },
      rule: {
        name: [
          { required: true, message: '药店名称不能为空', trigger: 'blur' }
        ],
        province: [
          { required: true, message: '省份不能为空', trigger: 'blur' }
        ],
        city: [
          { required: true, message: '市不能为空', trigger: 'blur' }
        ],
        district: [
          { required: true, message: '区域不能为空', trigger: 'blur' }
        ],
        leader: [
          { required: true, message: '负责人不能为空', trigger: 'blur' }
        ],
        leaderMobilePhone: [
          { required: true, message: '负责人电话不能为空', trigger: 'blur' }
        ],
        telephone: [
          { required: true, message: '联系方式不能为空', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '地址不能为空', trigger: 'blur' }
        ],
        businessHours: [
          { required: true, message: '营业时间不能为空', trigger: 'blur' }
        ],
        lngLat: [
          { required: false, message: '经纬度不能为空', trigger: 'blur' }
        ],
        remark: [
          { required: false, message: '备注不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    if (this.isEdit) {
      get({
        id: this.params.id
      }).then(res => {
        this.form = this.$util.copy(res.data, this.form)
      })
    }
  },
  methods: {
    submit () {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (this.isEdit) {
              update(this.form).then(res => {
                resolve(res)
              }).catch(e => {
                reject(e)
              })
            } else {
              delete this.form.id
              save(this.form).then(res => {
                resolve(res)
              }).catch(e => {
                reject(e)
              })
            }
          } else {
            reject(new Error('error'))
          }
        })
      })
    }
  }
}
</script>
